package com.silence.gmall.product.rpc;

import com.silence.gmall.annotations.Log;
import com.silence.gmall.base.Result;
import com.silence.gmall.product.domain.repository.BaseCategory1Repository;
import com.silence.gmall.product.domain.repository.SkuInfoRepository;
import com.silence.gmall.product.domain.repository.SkuSaleAttrValueRepository;
import com.silence.gmall.product.domain.repository.SpuSaleAttrRepository;
import com.silence.gmall.product.dto.SkuInfoDto;
import com.silence.gmall.product.dto.SpuSaleAttrDto;
import com.silence.gmall.product.service.SkuInfoService;
import com.silence.gmall.web.dto.CategoryViewDto;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;

/**
 * @Author silence
 * @Date 2025-08-28 16:25
 * @Email 792245022@qq.com
 * @Version 1.0
 * @Description
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/inner/product")
public class SkuDetailRpcController {

    private final BaseCategory1Repository baseCategory1Repository;
    private final SkuInfoService skuInfoService;
    private final SkuInfoRepository skuInfoRepository;
    private final SpuSaleAttrRepository spuSaleAttrRepository;
    private final SkuSaleAttrValueRepository skuSaleAttrValueRepository;

    @GetMapping("/skuInfo/{skuId}")
    @Log(interfaceName = "根据SKU ID查询详情信息")
    public Result<SkuInfoDto> getSkuInfo(@PathVariable("skuId") Long skuId) {
        return Result.build(skuInfoService.getSkuInfo(skuId));
    }

    @GetMapping("/skuInfo/getCategoryView/{category3Id}")
    @Log(interfaceName = "查询分类信息视图")
    Result<CategoryViewDto> getCategoryView(@PathVariable("category3Id") String category3Id) {
        return Result.build(baseCategory1Repository.getCategoryView(category3Id));
    }

    @GetMapping("/skuInfo/price/{skuId}")
    @Log(interfaceName = "获取SKU价格")
    Result<String> getSkuPrice(@PathVariable("skuId") Long skuId) {
        return Result.build(Optional.ofNullable(skuInfoRepository.getSkuInfoById(skuId))
                .map(SkuInfoDto::getPrice).orElse("0"));
    }

    @GetMapping("/skuInfo/getSpuSaleAttrAndValue/{spuId}/{skuId}")
    @Log(interfaceName = "获取SPU销售属性和值")
    Result<List<SpuSaleAttrDto>> getSpuSaleAttrAndValue(@PathVariable("spuId") Long spuId, @PathVariable("skuId") Long skuId) {
        return Result.build(spuSaleAttrRepository.spuSaleAttrList(spuId, skuId));
    }

    @GetMapping("/skuInfo/getSpuValuesSkuJson/{spuId}")
    @Log(interfaceName = "获取同一个SPU下面的所有SKU组合")
    Result<String> getSpuValuesSkuJson(@PathVariable("spuId") Long spuId) {
        return Result.build(skuSaleAttrValueRepository.getSpuValuesSkuJson(spuId));
    }
}
